﻿using System;
using System.Collections.Generic;
using System.Configuration;
using log4net;
using MyOrganizerModel.Database;

namespace MyOrganizerModel
{
    public sealed class BusinessModel
    {
        public static BusinessModel GetInstance { get { return Instance(); } }
        private static BusinessModel BusinessModelInstance = null;
        private static BusinessModel Instance()
        {
            if (null == BusinessModelInstance)
                BusinessModelInstance = new BusinessModel();
            return BusinessModelInstance;
        }

        DataModel Db = null;
        private BusinessModel()
        {
            Db = DataModel.GetInstance;
        }

        #region Books

        public List<Books> GetBooks()
        {
            return Db.GetBooks();
        }
        public List<Books> GetBooksByUserId(int UserId)
        {
            return Db.GetBooksByUserId(UserId);
        }
        public Books GetBookByBookId(int BookId)
        {
            return Db.GetBookByBookId(BookId);
        }
        public void DeleteBook(int BookId)
        {
            Db.DeleteBook(BookId);
        }
        public void AddBook(Books Book)
        {
            Db.AddBook(Book);
        }
        public void UpdateBook(Books Book)
        {
            Db.UpdateBook(Book);
        }

        #endregion

        #region Movies

        public List<Movies> GetMovies()
        {
            return Db.GetMovies();
        }
        public List<Movies> GetMoviesByUserId(int UserId)
        {
            return Db.GetMoviesByUserId(UserId);
        }
        public Movies GetMovieByMovieId(int MovieId)
        {
            return Db.GetMovieByMovieId(MovieId);
        }
        public void DeleteMovie(int MovieId)
        {
            Db.DeleteMovie(MovieId);
        }
        public void AddMovie(Movies Movie)
        {
            Db.AddMovie(Movie);
        }
        public void UpdateMovie(Movies Movie)
        {
            Db.UpdateMovie(Movie);
        }

        #endregion

        #region TvShows

        public List<TvShows> GetTvShows()
        {
            return Db.GetTvShows();
        }
        public List<TvShows> GetTvShowsByUserId(int UserId)
        {
            return Db.GetTvShowsByUserId(UserId);
        }
        public TvShows GetTvShowByTvShowId(int TvShowId)
        {
            return Db.GetTvShowsByTvShowId(TvShowId);
        }
        public void DeleteTvShow(int TvShowId)
        {
            Db.DeleteTvShow(TvShowId);
        }
        public void AddTvShow(TvShows TvShow)
        {
            Db.AddTvShow(TvShow);
        }
        public void UpdateTvShow(TvShows TvShow)
        {
            Db.UpdateTvShow(TvShow);
        }

        #endregion

        #region Users

        public int GetUser(string UserName, string Password)
        {
            return Db.GetUser(UserName, Password);
        }
        public Users GetUserById(int UserId)
        {
            return Db.GetUserById(UserId);
        }
        public void AddUser(Users User)
        {
            Db.AddUser(User);
        }
        public void UpdateUser(Users User)
        {
            Db.UpdateUser(User);
        }
        public bool ChangePassword(string OldPass, string NewPass, int UserId)
        {
            var UserInfo = GetUserById(UserId);
            if (!OldPass.Equals(UserInfo.Password)) { return false; }
            UserInfo.Password = NewPass;
            UpdateUser(UserInfo);
            return true;
        }
        public bool ChangeUsername(string OldUser, string NewUser, int UserId)
        {
            var UserInfo = GetUserById(UserId);
            if (!OldUser.Equals(UserInfo.Name)) { return false; }
            UserInfo.Name = NewUser;
            UpdateUser(UserInfo);
            return true;
        }
        public void AddReport(Reports Report)
        {
            Db.AddReport(Report);
        }
        public IList<Reports> GetReportsByUserId(int UserId)
        {
            return Db.GetReportsByUserId(UserId);
        }

        #endregion
    }

    public abstract class PlatformModel : System.Web.UI.Page
    {
        public BusinessModel Logic = BusinessModel.GetInstance;

        public int UserId
        {
            get
            {
                if (Session["UserId"] != null)
                {
                    return Int32.Parse(Session["UserId"].ToString());
                }
                return 0;
            }
        }

        //public string Seccao
        //{
        //    get
        //    {
        //        if (Page.RouteData.Values[ConstantsModel.Seccao] == null || Page.RouteData.Values[ConstantsModel.Seccao].ToString().Equals(string.Empty))
        //        {
        //            return ConstantsModel.SeccaoMasculina;
        //        }
        //        return Page.RouteData.Values[ConstantsModel.Seccao].ToString();
        //    }
        //}

        //public string IdAnuncio
        //{
        //    get
        //    {
        //        if (Page.RouteData.Values[ConstantsModel.IdAnuncio].ToString().Equals(string.Empty))
        //        {
        //            return ConstantsModel.IdAnuncioDefaultValue;
        //        }
        //        return Page.RouteData.Values[ConstantsModel.IdAnuncio].ToString();
        //    }
        //}

        //public string UserNameSessao
        //{
        //    get
        //    {
        //        if (Session["UserName"] == null || Session["UserName"].Equals(string.Empty))
        //        {
        //            return string.Empty;
        //        }
        //        return Session["UserName"].ToString();
        //    }
        //    set
        //    {
        //        Session["UserName"] = value;
        //    }
        //}

        //public string PasswordSessao
        //{
        //    get
        //    {
        //        if (Session["Password"] == null || Session["Password"].Equals(string.Empty))
        //        {
        //            return string.Empty;
        //        }
        //        return Session["Password"].ToString();
        //    }
        //    set
        //    {
        //        Session["Password"] = value;
        //    }
        //}

        //public string NomeSessao
        //{
        //    get
        //    {
        //        if (Session["Nome"].Equals(string.Empty))
        //        {
        //            return string.Empty;
        //        }
        //        return Session["Nome"].ToString();
        //    }
        //    set
        //    {
        //        Session["Nome"] = value;
        //    }
        //}

        //public string IdAnuncioSessao
        //{
        //    get
        //    {
        //        if (Session["IdAnuncio"].Equals(string.Empty))
        //        {
        //            return string.Empty;
        //        }
        //        return Session["IdAnuncio"].ToString();
        //    }
        //    set
        //    {
        //        Session["IdAnuncio"] = value;
        //    }
        //}

        //public bool VerificarSessao()
        //{
        //    if (UserNameSessao != string.Empty & PasswordSessao != string.Empty)
        //    {
        //        return true;
        //    }
        //    return false;
        //}
    }

    public static class LoggingModel
    {
        public static void Log(string Mode, Exception Ex, string Method)
        {
            ILog log = LogManager.GetLogger(Method);
            log4net.Config.XmlConfigurator.Configure();
            switch (Mode)
            {
                case "Error":
                    log.Error("Error", Ex);
                    break;
                case "Debug":
                    log.Debug("Debug", Ex);
                    break;
                case "Info":
                    log.Info("Info", Ex);
                    break;
                case "Warning":
                    log.Warn("Warning", Ex);
                    break;
                default:
                    log.Fatal("Fatal", Ex);
                    break;
            }
        }
    }

    public static class ConstantsModel
    {
        public static string LogMode = ConfigurationManager.AppSettings["LogMode"].ToString();
    }
}
